import { ElMessage } from 'element-plus';
import { useMemberMoneyApi } from '/@/api/member';
import { timeFormat } from '/@/utils/filters';
import { getStateFormatZh } from '/@/utils/commonRequest';
import { extractMoneyStatus, extractProject } from '/@/stores/dic';

const { getMoneyList } = useMemberMoneyApi();

export const useInit = () => {
	const tableData = ref([]);
	const formData = ref({
		keyWord: '',
		moneyStatus: '',
		project: '',
		registrationTime: null,
	});
	const page = () => {
		return {
			current: 1,
			pageSize: 10,
			total: 0,
		};
	};

	const ipage = ref(page());

	const handleGetMoneyList = async () => {
		let res = await getMoneyList({ ...formData.value, ...ipage.value });
		try {
			if (res.code === 200) {
				let { data, total } = res.data;
				tableData.value = data.map((item) => {
					item.add = item.addSub || 0;
					item.createTime = item.createTime || '';
					item.current = item.current || 0;
					item.email = item.email || '';
					item.endTime = item.endTime || '';
					item.noteTime = item.noteTime || '';
					item.id = item.id || '';
					item.moneyStatus = item.moneyStatus || 0;
					item.note = item.note || '';
					item.pageSize = item.pageSize || 0;
					item.project = item.project || '';
					item.startTime = item.startTime || '';
					item.updateTime = item.updateTime || '';
					item.sussTime = item.sussTime || '';
					item.userId = item.userId || 0;
					return item;
				});
				ipage.value.total = total;
			}
		} catch (err) {
			ElMessage.error('获取列表失败');
		}
	};

	const searchFun = () => {
		handleGetMoneyList();
	};

	return {
		ipage,
		tableData,
		formData,
		extractMoneyStatus,
		extractProject,
		timeFormat,
		handleGetMoneyList,
		getStateFormatZh,
		searchFun,
	};
};
